/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 Pentaho Corporation.  All rights reserved.
 *
 * @author Will Gorman (wgorman@pentaho.com)
 */
package org.pentaho.actioneditor.client.actionpanels;

import org.pentaho.actioneditor.client.ActionEditor;
import org.pentaho.actionsequence.dom.ActionInput;
import org.pentaho.actionsequence.dom.ActionInputConstant;
import org.pentaho.actionsequence.dom.IActionInput;
import org.pentaho.actionsequence.dom.IActionInputSource;
import org.pentaho.actionsequence.dom.IActionInputVariable;
import org.pentaho.actionsequence.dom.SimpleActionInputVariable;

import com.google.gwt.user.client.ui.TreeItem;

public abstract class AbstractActionPanel implements IActionPanel {
  protected TreeItem item;
  protected ActionEditor actionEditor;
  
  public void setTreeItem(TreeItem item) {
    this.item = item;
  }
  
  public IActionInputSource getProviderFromString(String text) {
    if (text.startsWith("<") && text.endsWith(">")) {
      return new SimpleActionInputVariable(text.substring(1, text.length() - 1), null);
    } else {
      return new ActionInputConstant(text, null);
    }
  }
  
  public String getActionInputString(IActionInput provider) {
    if (provider instanceof IActionInputVariable) {
      return "<" + ((IActionInputVariable)provider).getVariableName() + ">";
    } else if (provider instanceof ActionInput) {
      return "<" + ((ActionInput)provider).getMapping() + ">";
    } else if (provider instanceof ActionInputConstant) {
      return ((ActionInputConstant)provider).getStringValue();
    } else {
      throw new RuntimeException("Unknown provider type : " + provider);
    }
  }
}
